package com.gxuwz.medical.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.exception.DbException;

public class FamilyDao extends GenericDao<Family> {

	@Override
	protected Family handle(ResultSet rs) throws SQLException {

		String familycode = rs.getString("familycode");
		String familyname = rs.getString("familyname");
		String IDcard = rs.getString("IDcard");
		String familyaddr = rs.getString("familyaddr");
		String familynumber = rs.getString("familynumber");
		String familystate = rs.getString("familystate");
		Timestamp createtime = rs.getTimestamp("createtime");
		String userid = rs.getString("userid");
		String areacode = rs.getString("areacode");
		String familyteam = rs.getString("familyteam");

		Area area = new Area(areacode);
		Family entity = new Family(familycode, familyname, IDcard, familyaddr, familynumber, familystate, createtime,
				userid, area, familyteam);
		return entity;
	}

	public List<Family> findFamilyList() throws DbException {
		try {
			String sql = "select * from t_family where 1=1";

			Object[] params = {};

			return super.queryOjects(sql, params);

		} catch (SQLException e) {
			throw new DbException("按级别查找行政区域失败!", e);
		}
	}

	public List<Family> findByKeyValue(String key, String value) throws DbException {
		try {
			String sqlSB = "";
			StringBuffer sb = new StringBuffer(1024);
			if (key != null && value != null) {
				sqlSB = sb.append(" and " + key + "='" + value + "'").toString();
			}
			String sql = "select * from t_family where 1=1 " + sqlSB + "";

			Object[] params = {};

			return super.queryOjects(sql, params);

		} catch (SQLException e) {
			throw new DbException("慢性病证信息查询失败!", e);
		}
	}

	public List<Family> findFamilyListByFimalyID(String familycode) throws DbException {
		try {
			String sql = "select * from t_family where 1=1 and familycode='" + familycode + "'";

			Object[] params = {};

			return super.queryOjects(sql, params);

		} catch (SQLException e) {
			throw new DbException("按级别查找行政区域失败!", e);
		}
	}
}
